package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBUtil {
    private static String jdbcName;//描述驱动信息
    private static String dbUrl;
    private static String dbUsername;
    private static String dbPassword;
    //进行成员静态的初始化
    static {
        jdbcName ="com.mysql.jdbc.Driver";
        dbUrl="jdbc:mysql://localhost:3306/db_web";
        dbUsername="root";
        dbPassword="root";
        try {
            Class.forName(jdbcName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        Connection connection= DriverManager.getConnection(dbUrl,dbUsername,dbPassword);
        return connection;
    }
    public  static void closeConnection(Connection connection, PreparedStatement preparedStatement) throws SQLException {
        if(connection!=null){
            connection.close();
        }
        if (preparedStatement!=null){
            preparedStatement.close();
        }
    }

}
